import { observable, action, toJS } from 'mobx';
import * as API_LIST from '../apis/report';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { forEach, isEmpty } from 'lodash';
const { TableStore } = WeaTableNew;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
export default class Report {
  @observable title = `${getLabel(16613,'会议报表')}`;
  @observable userList = [];
  @observable setsearchtime = '3';
  @observable setsearchtype = '1';
  @observable absent = {};
  @observable resolut = {};
  @observable processd = {};
  @observable roomUsed = {};
  @observable reporttab = 1;
  @observable absentType = 1;
  @observable chartParams = {
    timeSag: '3',
    selectType: '1',
  };

  @observable dataKey = "";
  @observable tableStore = {};
  //用于控制高级搜索的显隐的变量
  @observable ifShowSearchAd = false;
  @observable adSearchForm = new WeaForm();
  @observable conditioninfo = [];

  @observable defdep = {};
  @observable defsub = {};

  @observable isInit = false;//用来判断是否初始化调用absentP
  @observable isWillMount = false;

  @observable condition = [];//查询条件

  @observable themeFontSize = 12; 

  @action
  addUser(params) {
    this.userList.push({ name: "dyf" });
  }

  getUserList(params) {
    this.userList.slice();
  }

  doLoading = loading => {
    this.loading = loading;
  }

  @action 
  setAbsentType=(value)=>{
    this.absentType=value;
  }

  //获取人员的所在部门和分部
  getUserDefInfo = (value = {}) => {
    API_LIST.getUserDefInfoP(value).then(data => {
      this.defsub = data.defsub;
      this.defdep = data.defdep;
      this.chartParams = { ...this.chartParams, subIds: data.defsub.id }
      if (this.isInit) {
        this.reporttab = 1;
        this.chartParams = {
          timeSag: '3',
          selectType: '1',
          subIds: data.defsub.id
        }
      }
      this.chartPaint();
    });
  }

  //e9会议报表-会议缺席统计
  absentP = (value = {}) => {
    value.absentType=this.absentType;
    API_LIST.absentP(value).then(data => {
      this.absent = data;
    });
  }

  //e9会议报表-会议缺席统计
  resolutP = (value = {}) => {
    API_LIST.resolutP(value).then(data => {
      this.resolut = data;
    });
  }

  //e9会议报表-会议任务统计
  processdP = (value = {}) => {
    API_LIST.processdP(value).then(data => {
      this.processd = data;
    });
  }
  //会议报表里的tab
  changeReportTab = value => {
    this.reporttab = value;
    this.chartPaint();
  }

  chartPaint = () => {
    let params = toJS(this.chartParams);
    if (this.reporttab == 1) {
      this.absentP(params)
    }
    if (this.reporttab == 2) {
      this.resolutP(params)
    }
    if (this.reporttab == 3) {
      this.processdP(params)
    }
    this.isInit = false;
  }



  roomUsedData = (value = {}) => {
    API_LIST.getRoomUsedData(value).then(data => {
      this.roomUsed = data;
    });
  }

  getRoomUsedTableData = (value = {}) => {
    this.loading = true;
    //调用API
    API_LIST.getTableDatas(value).then(action((data) => {
      this.tableStore = { ...this.tableStore, [data.sessionkey]: new TableStore() }
      this.tableStore[data.sessionkey].getDatas(data.sessionkey, 1);
      this.loading = false;
      this.dataKey = data.sessionkey;
    }));
  }

  @action
  exportRoomUsedExcel=()=>{
    this.tableStore[this.dataKey].exportAll()
  }
  
  setShowSearchAd(bool) {
    this.ifShowSearchAd = bool;
  }

  initCondition = params => {
    API_LIST.getCondition(params).then(action((data) => {
      const { condition } = data;
      this.adSearchForm.initFormFields(condition);
      this.conditioninfo = condition;
    }));
  }

  getRoomUsedCondition = (params = {}) => {
    API_LIST.getRoomUsedCondition(params).then(data => {
      if (data.api_status) {
        this.condition = data.items;
        let timeSag = '', selectType = '', subid = '', replaceDatas = data.items[1].selectLinkageDatas['1'].browserConditionParam.replaceDatas;
        forEach(data.items[0].options, (option) => {
          if (option.selected) {
            timeSag = option.key;
          }
        });
        forEach(data.items[1].options, (option) => {
          if (option.selected) {
            selectType = option.key;
          }
        });

        !isEmpty(replaceDatas) && (subid = replaceDatas[0].id);
        this.getRoomUsedTableData({ timeSag, selectType, subid });
        this.roomUsedData({ timeSag, selectType, subid });
      }
    });
  }

  init = () => {
    this.getRoomUsedCondition();
  }

  getMyTheme = params => {
    API_LIST.getMyTheme(params).then(action((res) => {
      this.themeFontSize = res.data.themeFontSize;
    }));
  }
}